﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data.SqlClient;
using Job.Web.Framework;
using System.Data;
using Job.Logic.Common;
using System.Web.Hosting;

namespace Job.Web.userControls.Public
{
    public partial class SubjectList : BaseCtrl
    {
        int subjectid = myR["subjectid"].IntValue;
        int categoryid = myR["categoryid"].IntValue;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindCount();
                this.BindData();
            }
        }

        private void BindCount()
        {
            //this.winPager1.CurrentPage = 1;
            //AspNetPagerInit.InitPager(pagerButtom);
            DataSet ds = this.GetData(1);
            int count = 0;
            if (ds != null)
            {
                count = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }

            if (count <= pagerTop.PageSize)
            {
                pagerTop.Visible = false;
                pagerButtom.Visible = false;
            }
            else
            {
                pagerButtom.RecordCount = count;
                pagerButtom.DataBind();
                pagerTop.RecordCount = count;
                pagerTop.DataBind();
            }
        }

        void BindData()
        {
            DataSet ds = GetData(0);
            if (ds != null)
            {
                this.dlList.DataSource = ds.Tables[0];
                this.dlList.DataBind();
            }
            else
            {
                this.pagerButtom.Visible = false;
                this.pagerTop.Visible = false;
            }
        }



        private DataSet GetData(int type)
        {
            ArrayList param = new ArrayList();

            if (subjectid > 0)
            {
                SqlParameter pm = new SqlParameter();
                pm.ParameterName = "subject_id";
                pm.Value = subjectid;
                pm.SqlDbType = SqlDbType.Int;
                param.Add(pm);
            }

            SubjectLogic blogic = new SubjectLogic();
            DataSet ds = blogic.GetBaseContentList(pagerButtom.PageSize, pagerButtom.CurrentPageIndex, type, this.SortField, this.SortDirection, param);
            return ds;
        }

        /// <summary>
        /// 排序方式
        /// </summary>
        public Job.Web.Framework.SortDirection SortDirection
        {
            get
            {
                return Job.Web.Framework.SortDirection.Descending;
            }
        }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string SortField
        {
            get
            {
                return "order_no,content_id";
            }
        }

        protected void dlList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink hlDetail = (HyperLink)e.Item.FindControl("hlDetail");
                        Label lbltime = (Label)e.Item.FindControl("lbltime");
                        Label lblid = (Label)e.Item.FindControl("lblid");
                        lbltime.Text =  "(" + lbltime.Text.Trim() + ")";
                        //int _subjectid = Constants.GetInfoIdByListId(subjectid);
                        //设置具体subject的URL用来显示Detail信息。
                        hlDetail.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid=" + categoryid + "&subjectid=" + subjectid + "&contentid=" + lblid.Text.Trim();
                        break;
                    }
            }
        }

        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void pagerTop_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void pagerButtom_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            pagerTop.CurrentPageIndex = e.NewPageIndex;
            Session["listcurrentPageIndex"] = e.NewPageIndex;
        }

        protected void pagerTop_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            pagerButtom.CurrentPageIndex = e.NewPageIndex;
            Session["listcurrentPageIndex"] = e.NewPageIndex;
        }
    }
}